package alon.xmnn.bobing;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.log4j.Logger;

import alon.app.itask.IBrowserLisener;
import alon.app.itask.TERequest;
import alon.app.itask.Task;
import alon.app.task.ActionPool;
import alon.app.tools.CodeUtil;

public class Xmnn extends Task implements IBobingFinish{
	private static final long serialVersionUID = 7287141644069963841L;
	public Logger logInfo = Logger.getLogger("app.info");
	public Logger logError = Logger.getLogger("app.error");
	public static String COOKIE_PREFIX="xmnn_";
	private String loginUrl="";
	public Random rand=new Random();
//	public String cktime="31536000";
	private List<NnAccount> users=new ArrayList<NnAccount>();
	public static String reportMail="";
	public Xmnn() {
		//cktime=String.valueOf(getCktime());
		setName("xmnn");
	}
	public synchronized void init() throws Exception {
		try {
			loadConfigFile("xmnn.ini");
			loginUrl=getConfValue("xmnn", "loginUrl", "http://bobing.xmnn.cn/unionpay/bobing.php");
			String userls=CodeUtil.decodeUnicode(getConfValue("xmnn", "userls", ""));
			String userlist[]=userls.split(";");
			for(int i=0;i<userlist.length;i++){
				String up=userlist[i];
				if(!"".equals(up)&&up.indexOf("/")>0){
					String uinfo[]=up.split("/");
					NnAccount account=new NnAccount();
					if(uinfo.length>=2&&!uinfo[0].isEmpty()&&!uinfo[1].isEmpty()){
						account.setUser(uinfo[0]);
						account.setPass(uinfo[1]);
						if(uinfo.length>2&&!uinfo[2].isEmpty()){
							if(!"".equals(uinfo[2])&&uinfo[2].trim().matches("[0-9]+")){
								account.setWillCore(Integer.parseInt(uinfo[2]));
							}else{
								account.setWillCore(0);
							}
						}
					}else{
						continue;
					}
					if(!users.contains(account)){
						users.add(account);
					}
				}
			}
			logInfo.info("用户数:"+users.size());
		} catch (Exception e) {
			throw new Exception("厦门网主程序初始化异常",e);
		}
	}
	/**
	 * 登录后的下个事件
	 */
	public void nextOperate(NnAccount curUser){
		try {
//			ActionPool.getInstance().addTask(new BobingMain(curUser, getIniFile()));
			ActionPool.getInstance().addTask(new Bobing2013(curUser, getIniFile(),this));
		} catch (Exception e) {
			logError.error("初始化博饼失败",e);
		}
	}

	public void taskCore() throws Exception {
		loginOne();
	}
	public void taskDestroy() throws Exception {
	}
//	private long getCktime()
//	{
//		Calendar cal = Calendar.getInstance();
//		int year = cal.get(Calendar.YEAR);
//		if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
//			return 366*24*60*60;
//		}else{
//			return 365*24*60*60;
//		}
//	}
	public void loginOne(){
		if(users.size()>0){
			login(users.get(0));
			gotoSleep(5+rand.nextInt(5));
		}
	}
	public void login(NnAccount curUser){
		System.out.println("登录:"+loginUrl+"(用户名:"+curUser.getUser()+")");
		TERequest rq=new TERequest( Xmnn.COOKIE_PREFIX+curUser.getUser(),
				loginUrl, TERequest.METHOD_POST,"gb2312", "gb2312");
		rq.addParam("type","厦门网会员");
		rq.addParam("username",curUser.getUser());
		rq.addParam("password", curUser.getPass());
		doConnect(new IBrowserLisener(){
			private NnAccount curUser;
			private IBrowserLisener addUser(NnAccount curUser){
				this.curUser=curUser;
				return this;
			}
			public void doException(String msg,Exception e){
				logError.error(msg,e);
				login(curUser);
			}
			public void response(String resp)
			{
				if(resp.equals("0")||resp.equals("")){
					logInfo.info(curUser.getUser()+",您已经顺利登录");
					nextOperate(curUser);
					return;
				}else if(resp.indexOf("用户名或者密码错误")>0){
					logInfo.info(curUser.getUser()+",用户名或者密码错误");
					return;
				}else{
					logError.error(curUser.getUser()+",登录出错:"+resp);
				}
			}
		}.addUser(curUser), rq,iapp.getProxy());
	}
	public void taskError(String error, Exception e) {
	}
	public void finish(NnAccount ac) {
		if(users.contains(ac)){
			users.remove(ac);
		}
		loginOne();
	}
	
}
